package com.learn.demo1;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.binding.BinderAwareChannelResolver;
import org.springframework.messaging.Message;
import org.springframework.messaging.SubscribableChannel;
import org.springframework.messaging.support.MessageBuilder;

@SpringCloudApplication
@EnableBinding(SimpleSourse.class)
public class DynamicApplication implements CommandLineRunner{

    public static void main(String[] args) {
        SpringApplication.run(DynamicApplication.class, args);
    }


    @Autowired
    BinderAwareChannelResolver resolver;

    @Autowired
    @Qualifier(SimpleSourse.OUTPUT_NAME)
    SubscribableChannel subscribableChannel;

    @Override
    public void run(String... strings) throws Exception {
        System.err.println("subscribe message :" + subscribableChannel.subscribe(message -> {
            System.err.println("  message payload is :" + message.getPayload());
        }));

        subscribableChannel.send(MessageBuilder.withPayload("test self").build());
        System.err.println("send message success");
    }

    @StreamListener(SimpleSourse.OUTPUT_NAME)
    public void jieshou(Message<String> message){

        System.err.println("注解的方式接收到了");
        System.err.println(message.getPayload());
    }
}
